import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;

import org.junit.Assert;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import data.BooleanArrayContainer;
import data.ByteArrayContainer;
import data.Constants;
import data.ShortArrayContainer;


public class XStreamArrayUnitTest {

	public static void main(String[] args) {
		XStream stream = new XStream(new DomDriver("UTF-8"));
		boolean[] boolVals = new boolean[Constants.ITERATIONS];
		for (int i = 0; i < Constants.ITERATIONS; i++) {
			boolVals[i] = i % 2 == 0;
		}

		byte[] byteVals = new byte[Constants.ITERATIONS];
		for (int i = 0; i < Constants.ITERATIONS; i++) {
			byteVals[i] = (byte) i;
		}

		short[] values = new short[Constants.ITERATIONS];
		for (int i = 0; i < Constants.ITERATIONS; i++) {
			values[i] = (short) (i + Short.MAX_VALUE / 2);
		}
		BooleanArrayContainer booleancontainer = new BooleanArrayContainer();
		booleancontainer.values = boolVals;
		final ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
		stream.toXML(booleancontainer, out);
		System.out.println(out);
		ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
		BooleanArrayContainer readBooleanContainer = (BooleanArrayContainer) stream.fromXML(in);
		Assert.assertTrue(Arrays.equals(boolVals, readBooleanContainer.values));
		System.out.println("OK");

		ByteArrayContainer bytecontainer = new ByteArrayContainer();
		bytecontainer.values = byteVals;
		out.reset();
		stream.toXML(bytecontainer, out);
		System.out.println(out);
		in = new ByteArrayInputStream(out.toByteArray());
		ByteArrayContainer readByteContainer = (ByteArrayContainer) stream.fromXML(in);
		Assert.assertTrue(Arrays.equals(byteVals, readByteContainer.values));
		System.out.println("OK");
		
		ShortArrayContainer container = new ShortArrayContainer();
		container.values = values;
		out.reset();
		stream.toXML(container, out);
		System.out.println(out);
		in = new ByteArrayInputStream(out.toByteArray());
		ShortArrayContainer readShortContainer = (ShortArrayContainer) stream.fromXML(in);
		Assert.assertArrayEquals(values, readShortContainer.values);
		System.out.println("OK");
	}

}
